package com.atguigu.demo.entity;

import java.util.List;

public class Page<T> {

    public static final Integer DEFAULT_PAGE_NUM = 1;
    public static final Integer DEFAULT_PAGE_SIZE = 10;

    private Integer pageNum;
    private Integer pageSize;
    private Integer totalRecord;
    private Integer totalPage;
    private List<T> list;

    public Page(Integer pageNum, Integer pageSize, Integer totalRecord) {
        // 没有符合查询条件的记录，不执行计算
        if (totalRecord <= 0) {
            this.pageNum = DEFAULT_PAGE_NUM;
            this.pageSize = DEFAULT_PAGE_SIZE;
            this.totalRecord = totalRecord;
            this.totalPage = 1;
            return ;
        }

        // 计算总页数
        this.totalPage = (totalRecord + pageSize - 1) / pageSize;

        // 修正页码
        if (pageNum != null && pageNum < 1) pageNum = 1;
        if (pageNum != null && pageNum > totalPage) pageNum = totalPage;

        // 给各个属性赋值
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalRecord = totalRecord;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public boolean isHasPrev() {
        return pageNum > 1;
    }

    public boolean isHasNext() {
        return pageNum < totalPage;
    }

    public int getPrev() {
        return pageNum - 1;
    }

    public int getNext() {
        return pageNum + 1;
    }
}